﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Data;
using WeatherPlugin;


namespace AHB
{
    public partial class Collection : System.Web.UI.Page
    {
        bool TableContains(string Table, string Field, string Value)
        {
            SqlDataSource1.SelectCommand = "SELECT " + Field + " FROM " + Table + " WHERE " + Field + "='" + Value + "'";
            DataView View = (DataView)SqlDataSource1.Select(new DataSourceSelectArguments());
            if (View.Count == 0)
                return false;
            else
                return true;
        }

        int StateFromHiveData(string Site)
        {
            return 0;
        }


        protected void Page_Load(object sender, EventArgs e)
        {

            string sResponse = "FAILED (UNKNOWN)";

            SqlDataSource1.SelectCommand = "SELECT PID FROM Workers WHERE PID='" + Request.QueryString["PID"] + "'";
            DataView View = (DataView)SqlDataSource1.Select(new DataSourceSelectArguments());
            if (View.Count != 1)
                sResponse = "FAILED (NOT AUTHORIZED)";
            else
            {
                SqlDataSource1.SelectCommand = "SELECT Hive_ID FROM Hives WHERE Hive_ID='" + Request.QueryString["HiveID"] + "'";
                View = (DataView)SqlDataSource1.Select(new DataSourceSelectArguments());
                if (View.Count != 1)
                    sResponse = "FAILED (HIVE NOT REGISTERED)";
                else
                {
                    SqlDataSource1.SelectCommand = "SELECT WID FROM Workers WHERE PID='" + Request.QueryString["PID"] + "'";
                    View = (DataView)SqlDataSource1.Select(new DataSourceSelectArguments());
                    string WID = View[0]["WID"].ToString();

                    SqlDataSource1.SelectCommand = "SELECT WID, Hive_ID FROM Hives WHERE Hive_ID='" + Request.QueryString["HiveID"] + "' AND WID='" + WID + "'";
                    View = (DataView)SqlDataSource1.Select(new DataSourceSelectArguments());
                    try
                    {
                        if (View.Count != 1)
                            sResponse = "FAILED - YOU ARE NOT BEE KEEPER FOR THIS HIVE";
                        else
                        {
                            SqlDataSource1.InsertCommand = "INSERT INTO HiveData (HiveID, Date, Time, Rainfall, TempMax, TempMin, SupersCount, FrameCount, FoodLeft, Feed, Queenright, Problems, Manipulation, Swap, Comments, UUID) VALUES (@HiveID, @Date, @Time, @Rainfall, @TempMax, @TempMin, @SupersCount, @FrameCount, @FoodLeft, @Feed, @Queenright, @Problems, @Manipulation, @Swap, @Comments, NEWID())";
                            SqlDataSource1.InsertParameters.Add("HiveID", Request.QueryString["HiveID"]);
                            SqlDataSource1.InsertParameters.Add("Date", DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToShortTimeString());
                            SqlDataSource1.InsertParameters.Add("Time", DateTime.Now.ToShortTimeString());
                            SqlDataSource1.InsertParameters.Add("Rainfall", "0");
                            SqlDataSource1.InsertParameters.Add("TempMax", "0");
                            SqlDataSource1.InsertParameters.Add("TempMin", "0");
                            SqlDataSource1.InsertParameters.Add("SupersCount", Request.QueryString["SupersCount"]);
                            SqlDataSource1.InsertParameters.Add("FrameCount", Request.QueryString["FrameCount"]);
                            SqlDataSource1.InsertParameters.Add("FoodLeft", (int.Parse(Request.QueryString["FoodLeft"]) * 0.2).ToString());
                            SqlDataSource1.InsertParameters.Add("Feed", (int.Parse(Request.QueryString["Feed"]) * 0.2).ToString());
                            SqlDataSource1.InsertParameters.Add("Queenright", Request.QueryString["Queenright"]);
                            SqlDataSource1.InsertParameters.Add("Problems", Request.QueryString["Problems"]);
                            SqlDataSource1.InsertParameters.Add("Manipulation", Request.QueryString["Manipulation"]);
                            SqlDataSource1.InsertParameters.Add("Swap", Request.QueryString["SwapID"]);
                            SqlDataSource1.InsertParameters.Add("Comments", Request.QueryString["Comment"]);
                            SqlDataSource1.Insert();

                            if (Request.QueryString["GPS"] != "UNDEFINED")
                            {
                                //The device uploaded the coords of the hive, update it and do a check to make sure the hive has not moved too far away and if it has send out notification.
                                SqlDataSource1.UpdateCommand = "UPDATE Hives SET Location='" + Request.QueryString["GPS"] + "' WHERE Hive_ID ='" + Request.QueryString["HiveID"] + "'";
                                SqlDataSource1.Update();
                            }
                            sResponse = "SUCCESSFUL";
                        }
                    }
                    catch (Exception ex)
                    {
                        sResponse = "FAILED -  SERVER-SIDE ERROR";
                    }
                }
            }
           Response.Clear();
           Response.ClearContent();
           Response.ClearHeaders();
           Response.ContentType = "text/plain"; 
           Response.Write(sResponse);
           Response.End();
        }
        }
    }
